//
//  NSDictionary+Accessors.m
//  Belle
//
//  Created by Allen Hsu on 1/11/14.
//  Copyright (c) 2014 Allen Hsu. All rights reserved.
//

#import "NSDictionary+Accessors.h"

@implementation NSDictionary (Accessors)

- (NSString *)ac_norStringForKey:(NSString *)key{
    
    if (!self) {
        return @"";
    }
    
    id value = [self objectForKey:key];
    
    if ([value isKindOfClass:[NSString class]]) {
        return value;
    }else if ([value isKindOfClass:[NSNumber class]]) {
        NSNumber *number = (NSNumber *)value;
        return number.stringValue;
    } else if ([value respondsToSelector:@selector(description)]) {
        
        if ([@"<null>" isEqualToString:[value description]]) {
            return @"";
        }
        if ([@"null" isEqualToString:[value description]]) {
            return @"";
        }
        
        return [value description];
    }
    
    return @"";
}

- (NSString *)ac_stringForKey:(NSString *)key
{
    id value = [self objectForKey:key];
    
    if ([value isKindOfClass:[NSString class]]) {
        return value;
    }else if ([value isKindOfClass:[NSNumber class]]) {
        NSNumber *number = (NSNumber *)value;
        return number.stringValue;
    } else if ([value respondsToSelector:@selector(description)]) {

        if ([@"<null>" isEqualToString:[value description]]) {
            return nil;
        }
        if ([@"null" isEqualToString:[value description]]) {
            return nil;
        }
        
        return [value description];
    }
    
    return nil;
}


- (NSArray *)ac_arrayForKey:(NSString *)key
{
    id value = [self objectForKey:key];

    if ([value isKindOfClass:[NSArray class]]) {
        return value;
    }

    return nil;
}

- (NSDictionary *)ac_dictionaryForKey:(NSString *)key
{
    id value = [self objectForKey:key];

    if ([value isKindOfClass:[NSDictionary class]]) {
        return value;
    }

    return nil;
}

- (NSNumber *)ac_numberForKey:(NSString *)key
{
    id value = [self objectForKey:key];

    if ([value isKindOfClass:[NSNumber class]]) {
        return value;
    } else if ([value isKindOfClass:[NSString class]]) {
        NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
        return [nf numberFromString:value];
    }

    return nil;
}

- (double)ac_doubleForKey:(NSString *)key
{
    id value = [self objectForKey:key];

    if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
        return [value doubleValue];
    }

    return 0;
}

- (float)ac_floatForKey:(NSString *)key
{
    id value = [self objectForKey:key];

    if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
        return [value floatValue];
    }

    return 0;
}

- (int)ac_intForKey:(NSString *)key
{
    id value = [self objectForKey:key];

    if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
        return [value intValue];
    }

    return 0;
}

- (unsigned int)ac_unsignedIntForKey:(NSString *)key
{
    id value = [self objectForKey:key];

    if ([value isKindOfClass:[NSString class]]) {
        NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
        value = [nf numberFromString:value];
    }

    if ([value isKindOfClass:[NSNumber class]]) {
        return [value unsignedIntValue];
    }

    return 0;
}

- (NSInteger)ac_integerForKey:(NSString *)key
{
    id value = [self objectForKey:key];

    if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
        return [value integerValue];
    }

    return 0;
}

- (NSUInteger)ac_unsignedIntegerForKey:(NSString *)key
{
    id value = [self objectForKey:key];

    if ([value isKindOfClass:[NSString class]]) {
        NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
        value = [nf numberFromString:value];
    }

    if ([value isKindOfClass:[NSNumber class]]) {
        return [value unsignedIntegerValue];
    }

    return 0;
}

- (long long)ac_longLongForKey:(NSString *)key
{
    id value = [self objectForKey:key];

    if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
        return [value longLongValue];
    }

    return 0;
}

- (unsigned long long)ac_unsignedLongLongForKey:(NSString *)key
{
    id value = [self objectForKey:key];

    if ([value isKindOfClass:[NSString class]]) {
        NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
        value = [nf numberFromString:value];
    }

    if ([value isKindOfClass:[NSNumber class]]) {
        return [value unsignedLongLongValue];
    }

    return 0;
}

- (BOOL)ac_boolForKey:(NSString *)key
{
    id value = [self objectForKey:key];

    if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
        return [value boolValue];
    }

    return NO;
}

- (NSString *)transformJSON
{
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString;
    if (!jsonData) {
        NSLog(@"%@",error);
    }else{
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range = {0,jsonString.length};
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0,mutStr.length};
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
}

- (NSString *)stampToDate:(NSString *)key formatter:(NSString *)formatter{
    
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateStyle:NSDateFormatterMediumStyle];
    [dateformatter setTimeStyle:NSDateFormatterShortStyle];
    [dateformatter setDateFormat:(formatter)?:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeInterval interval  = [self ac_doubleForKey:key];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:interval];
    NSString *confromTimespStr = [dateformatter stringFromDate:confromTimesp];
    return confromTimespStr;
}


- (NSString *)ac_amount:(NSString *)key{
    return [[self ac_norStringForKey:key] stringByAppendingString:@"$"];
}


- (NSInteger)getApiCode 
{
    
    NSString *value = [self objectForKey:@"retCode"];
    
    return value.integerValue;
}

- (NSString *)getApiMsg  
{
    
    return [self objectForKey:@"retMsg"];
}

- (id)getApiResult  
{
    
    id data = [self objectForKey:@"body"];
    
    if ([data isKindOfClass:[NSNull class]]) {
        
        return nil;
    }
    
    return data;
}

- (BOOL)judgeApiCode
{
    if ([self ac_intForKey:@"retCode"] == 0) {
        
        return YES;
    }
    
    return NO;
}

- (NSString *)valueStrByAsc {
    NSArray *sortedKeys = [self keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSString *srot1 = [NSString stringWithFormat:@"%@",obj1];
        NSString *srot2 = [NSString stringWithFormat:@"%@",obj2];
        return [srot1 compare:srot2];
    }];
    NSMutableArray *sortedValues = [[NSMutableArray alloc] init];
    for (NSString *key in sortedKeys) {
        [sortedValues addObject:[NSString stringWithFormat:@"%@", [self objectForKey:key]]];
    }
    NSString *sortedValuesString = [sortedValues componentsJoinedByString:@""];
    return sortedValuesString;
}

@end
